package handler

import (
	"context"
	"encoding/json"
	"fmt"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
	"mxshop_srvs/goods_srv/global"
	"mxshop_srvs/goods_srv/model"
	"mxshop_srvs/goods_srv/proto"
)

/*
*
商品分类
*/
func (g *GoodsServer) GetAllCategorysList(ctx context.Context, e *emptypb.Empty) (*proto.CategoryListResponse, error) {
	var Categorys []model.Category
	//SubCategory.SubCategory可以多取出一层  SubCategory.SubCategory可以取出两层
	global.DB.Where(&model.Category{Level: 1}).Preload("SubCategory.SubCategory").Find(&Categorys)
	b, _ := json.Marshal(&Categorys)
	return &proto.CategoryListResponse{JsonData: string(b)}, nil
}

/*
*
根据当前分类的id 查询子分类的信息
*/
func (g *GoodsServer) GetSubCategory(ctx context.Context, req *proto.CategoryListRequest) (*proto.SubCategoryListResponse, error) {
	subCategoryListResponse := proto.SubCategoryListResponse{}
	var category model.Category
	if result := global.DB.First(&category, req.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "当前分类不存在")
	}
	subCategoryListResponse.Info = &proto.CategoryInfoResponse{
		Id:             category.ID,
		Name:           category.Name,
		ParentCategory: category.ParentCategoryId,
		Level:          category.Level,
		IsTab:          category.IsTab,
	}
	var subCategorys []model.Category                         //查询子类信息
	var subCategoryInfoResponse []*proto.CategoryInfoResponse //定义最终的子类信息
	global.DB.Where(&model.Category{ParentCategoryId: req.Id}).Find(&subCategorys)
	for _, subCategory := range subCategorys {
		subCategoryInfoResponse = append(subCategoryInfoResponse, &proto.CategoryInfoResponse{
			Id:             subCategory.ID,
			Name:           subCategory.Name,
			ParentCategory: subCategory.ParentCategoryId,
			Level:          subCategory.Level,
			IsTab:          subCategory.IsTab,
		})
	}
	subCategoryListResponse.SubCategorys = subCategoryInfoResponse
	length := len(subCategoryInfoResponse)
	subCategoryListResponse.Total = int32(length)
	//获取当前分类的子分类
	return &subCategoryListResponse, nil
}

/*
*
添加分类
*/
func (g *GoodsServer) CreateCategory(ctx context.Context, req *proto.CategoryInfoRequest) (*proto.CategoryInfoResponse, error) {
	//采用create的形式 map形式的创建分类不给添加上add_time update_time
	//param := map[string]interface{}{}
	/*param := make(map[string]interface{})
	param["name"] = req.Name
	if req.Level != 1 {
		param["parent_category_id"] = req.ParentCategory
	}
	param["level"] = req.Level
	param["is_tab"] = req.IsTab*/
	//result := global.DB.Model(&category).Create(param)
	var category model.Category
	category.Name = req.Name
	if req.Level != 1 {
		category.ParentCategoryId = req.ParentCategory
	}
	category.Level = req.Level
	category.IsTab = req.IsTab
	result := global.DB.Save(&category)
	fmt.Println(result)
	return &proto.CategoryInfoResponse{
		Id:             category.ID,
		Name:           req.Name,
		ParentCategory: req.ParentCategory,
		Level:          req.Level,
		IsTab:          req.IsTab,
	}, nil
}

/*
*
删除分类
*/
func (g *GoodsServer) DeleteCategory(ctx context.Context, req *proto.DeleteCategoryRequest) (*emptypb.Empty, error) {
	if result := global.DB.Delete(&model.Category{}, req.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "删除失败")
	}
	return &emptypb.Empty{}, nil
}

/*
*
修改分类
*/
func (g *GoodsServer) UpdateCategory(ctx context.Context, req *proto.CategoryInfoRequest) (*emptypb.Empty, error) {
	var category model.Category
	if result := global.DB.First(&category, req.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "当前分类找不到")
	}
	if req.Name != "" {
		category.Name = req.Name
	}
	if req.Level != 0 {
		category.Level = req.Level
	}
	if req.IsTab {
		category.IsTab = req.IsTab
	}
	if req.ParentCategory != 0 {
		category.ParentCategoryId = req.ParentCategory
	}
	global.DB.Save(&category)
	return &emptypb.Empty{}, nil
}
